﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Xml.Linq;

public partial class admin_CateProd_Details : System.Web.UI.Page
{
  ProdCateBLL data = new ProdCateBLL();
  private static int maxLevel;
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        Session["url_back_admin"] = Request.RawUrl;

        maxLevel = GetLevelConfig();
        LoadTitle();
        LoadProdCate();
        if (Request.QueryString["Id"] != null)
          Filldata();
      }
    }
    private void Filldata()
    {
      int id = Convert.ToInt32(Request.QueryString["Id"]);
      ProdCate prodCate = data.GetById(id);
      txtName.Text = prodCate.Name;
      txtDescription.Text = prodCate.Description;
      dropParent.SelectedValue = prodCate.Parent.ToString();
      txtLink.Text = prodCate.Link;
      if (prodCate.isEnable) radioEnable.Checked = true;
      else radioVisible.Checked = true;
    }
    private void ShowChild(int parentId, string space)
    {
      List<ProdCate> listDataSource = data.GetByParent(parentId);

      foreach (ProdCate item in listDataSource)
      {
        ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
        dropParent.Items.Add(obj);

        ShowChild(item.Id, space + "- - - ");
      }
    }
    private void LoadProdCate()
    {
      dropParent.Items.Clear();
      ShowChild(0, " - - ");
      ListItem itemRoot = new ListItem("Danh mục gốc", "0");
      dropParent.Items.Insert(0, itemRoot);
    }
    private void LoadTitle()
    {
      if (Request.QueryString["Id"] != null)
      {
        h3Title.InnerText = "Chỉnh sửa danh mục";        
      }
    }
    private bool isValidate()
    {
      if (txtName.Text == "")
      {
        Myfunction.ThongBao(lbThongBao, "Tên danh mục không được để trống !");
        return false;
      }
      return true;
    }
    private void ResetForm()
    {
      txtName.Text = "";
      txtDescription.Text = "";
      radioEnable.Checked = true;
    }
    private int GetLevelConfig()
    {
      XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.XML");
      var xLevel = xElement.Descendants("MaxLevel").Descendants("CategoryProduct");
      XElement level = xLevel.First();
      return Convert.ToInt32(level.Value);
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
        return;
      }
      try
      {
        lbThongBao.Text = "";
        if (isValidate())
        {
          if (Request.QueryString["Id"] == null)
          {
            Add();
            ResetForm();
            LoadProdCate();
          }
          else
          {
            Update();           
          }         
        }
      }
      catch (Exception ex)
      {
        Myfunction.ThongBao(lbThongBao, ex.Message);
      }
    }
    private void Update()
    {
      int idParent = Convert.ToInt32(dropParent.SelectedValue);
      int id = Convert.ToInt32(Request.QueryString["Id"]);
      ProdCate ProdCate = new ProdCate();
      ProdCate ProdCateCurrent = data.GetById(id);

      ProdCate.Id = id;
      ProdCate.Name = txtName.Text;
      ProdCate.Description = txtDescription.Text;
      ProdCate.Sort = data.GetMaxSort(idParent);
      ProdCate.CreatedDate = ProdCateCurrent.CreatedDate;
      ProdCate.isEnable = (radioEnable.Checked ? true : false);
      ProdCate.Parent = idParent;
      ProdCate.isDelete = ProdCateCurrent.isDelete;
      ProdCate.Link = txtLink.Text;
      if (fileUpload1.HasFile)
      {
          if (ProdCate.Icon == null || ProdCate.Icon == "")
          {
              ProdCate.Icon = Myfunction.CopyFile(fileUpload1.PostedFile, "Images/CateProd", ProdCate.Name + "1");
          }
          else
          {
              ProdCate.Icon = Myfunction.UpdateFile(fileUpload1.PostedFile, "Images/CateProd", ProdCate.Image, ProdCate.Name + "1");
          }
      }
      if (idParent == 0) ProdCate.Level = 1;
      else
      {
        ProdCate.Level = (byte)(data.GetById(idParent).Level + 1);
        if (ProdCate.Level > maxLevel)
        {
          Myfunction.ThongBao(lbThongBao,"Danh mục chỉ được tối đa " + maxLevel + " cấp");
          return;
        }
      }

      ProdCate.Image = ProdCateCurrent.Image;
      if (radioDeAnh.Checked)
      {
        if (fileUpload.HasFile)
        {
            if (ProdCate.Image == null || ProdCate.Image == "")
            {
                ProdCate.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/CateProd", ProdCate.Name);
            }
            else
            {
                ProdCate.Image = Myfunction.UpdateFile(fileUpload.PostedFile, "Images/CateProd", ProdCate.Image, ProdCate.Name);
            }
        }
      }
     
      data.Update(ProdCate);

      Myfunction.ThongBao(lbThongBao,"Danh mục đã được cập nhật");
    }
    private void Add()
    {
      int idParent = Convert.ToInt32(dropParent.SelectedValue);
      ProdCate ProdCate = new ProdCate();

      ProdCate.Name = txtName.Text;
      ProdCate.Description = txtDescription.Text;
      ProdCate.Sort = data.GetMaxSort(idParent);
      ProdCate.CreatedDate = DateTime.Now;
      ProdCate.isEnable = (radioEnable.Checked ? true : false);
      ProdCate.Parent = idParent;
      ProdCate.isDelete = true;
      ProdCate.Link = txtLink.Text;
      if (fileUpload1.HasFile)
      {
          ProdCate.Icon = Myfunction.CopyFile(fileUpload1.PostedFile, "Images/CateProd", ProdCate.Name + "1");
      }

      if (idParent == 0) ProdCate.Level = 1;
      else
      {
        ProdCate.Level = (byte)(data.GetById(idParent).Level + 1);
        if (ProdCate.Level > maxLevel)
        {
         Myfunction.ThongBao(lbThongBao,"Danh mục chỉ được tối đa " + maxLevel + " cấp");
          return;
        }
      }
      if (radioDeAnh.Checked)
      {
        if (fileUpload.HasFile)
        {
            ProdCate.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/CateProd", ProdCate.Name);
        }
      }
      data.Add(ProdCate);
      Myfunction.ThongBao(lbThongBao, "Danh mục đã được cập nhật");
    }
    protected void radioBoAnh_CheckedChanged(object sender, EventArgs e)
    {
      if (radioBoAnh.Checked)
      {
        trFileUpload.Visible = false;
      }
      else
        trFileUpload.Visible = true;
    }
}